

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()	

	self.Flame 	= Material( "particles/fire1" )
	self.Glow 	= Material( "particle/fire" )
	
	self.StartTime = CurTime()
	
	self.GlowSize 	= math.Rand( 60, 100 )
	
	self.Entity:SetCollisionBounds( Vector( -32, -32, -32 ), Vector( 32, 32, 0 ) )
	
	self.Entity:SetMoveType( MOVETYPE_NONE )
	self.Entity:SetSolid( SOLID_NONE )
	
end


local FADETIME = 0.5

/*---------------------------------------------------------
   Name: DrawPre
---------------------------------------------------------*/
function ENT:Draw()

	if (self.Fires == nil) then return end
	
	local DieTime		= self.Entity:GetNetworkedFloat( 0 )
	
	if ( self.DownTrace == nil ) then
		local trace = {}
			trace.start 	= self.Entity:GetPos()
			trace.endpos 	= trace.start - self.Entity:GetAngles():Forward() * 128
			trace.filter 	= self.Entity
		self.DownTrace = util.TraceLine( trace )
	end
	
	local Mul = 1
	
	if ( self.StartTime + FADETIME > CurTime() ) then
		Mul = (self.StartTime + FADETIME) - CurTime()
		Mul = Mul / FADETIME
		Mul = 1 - Mul
	end
	
	if ( DieTime - FADETIME < CurTime() ) then
		Mul = CurTime() - (DieTime - FADETIME)
		Mul = Mul / FADETIME
		Mul = 1 - Mul
	end
	
	self.Angles 		= self.Entity:GetAngles()
	local Pos	 		= self.Entity:GetPos()
	local PlyAngles 	= LocalPlayer():GetAngles()
	self.ViewNormal 	= PlyAngles:Forward()
	
	self.Flame:SetMaterialFloat( "$alpha", Mul )
	render.SetMaterial( self.Flame )
		
	for k, v in pairs(self.Fires) do
		self:DrawFire( v, Mul )
	end

	self.Glow:SetMaterialFloat( "$alpha", Mul )
	render.SetMaterial( self.Glow )
	
	local SizeFlicker = math.Rand( 1, 30 )
	render.DrawQuadEasy( Pos - self.Angles:Forward() * 32 ,
					 self.DownTrace.HitNormal,
					 self.GlowSize + SizeFlicker, self.GlowSize + SizeFlicker,
					 Color( 255, 200-SizeFlicker, 100, Mul*255 ) )

end


function ENT:DrawFire( tab, mul )

	//Makin my own fire.
	self.Delay = self.Delay or 0
	
	if self.Delay == 2 then
	
		self.Delay = 0
		
		local Velocity = Vector(0,0,1)
		local rvec = Vector(math.Rand(-5,5),math.Rand(-5,5),0)
		
		local emitter = ParticleEmitter( tab.Pos )
		
		local particle = emitter:Add( "effects/muzzleflash"..math.random(1,4), tab.Pos+rvec )
			particle:SetVelocity( (Velocity * math.Rand( 10, tab.Height )) + WindVector )
			particle:SetDieTime( math.Rand( 0.1, tab.DieTime ) )
			particle:SetStartAlpha( math.Rand( 100, 170 ) )
			particle:SetEndAlpha( math.Rand( 10, 30 ) )
			particle:SetStartSize( math.Rand( 5, tab.Width ) )
			particle:SetEndSize( math.Rand( 15, 35 ) )
			particle:SetRoll( math.Rand( 0, 360 ) )
			particle:SetRollDelta( math.Rand( -0.1, 0.1 ) )
			particle:SetColor( math.Rand( 150, 255 ), math.Rand( 100, 150 ), 100 )
			particle:VelocityDecay( false )
		
		if math.random(1,30) == 1 then	
			local particle = emitter:Add( "particle/particle_smokegrenade", tab.Pos + (Vector(0,0,1) * math.Rand(50,70)) )
				particle:SetVelocity( (Velocity * math.Rand( 25, 45 )) + WindVector )
				particle:SetDieTime( math.Rand( 6, 8 ) )
				particle:SetStartAlpha( math.Rand( 50, 90 ) )
				particle:SetEndAlpha( math.Rand( 1, 5 ) )
				particle:SetStartSize( math.Rand( 10, 20 ) )
				particle:SetEndSize( math.Rand( 50, 150 ) )
				particle:SetRoll( math.Rand( 0, 360 ) )
				particle:SetRollDelta( math.Rand( -0.1, 0.1 ) )
				particle:SetColor( 40,40,40 )
				particle:VelocityDecay( false )
		end
		emitter:Finish()
	else
		self.Delay = self.Delay + 1
	end
end


function ENT:Think()

	if (self.Fires != nil) then return end

	self.Fires = {}
	
	local fire = {}
		fire.Pos 	= self:GetPos() - Vector(0,0,32)
		//fire.Width 	= math.Rand( 32, 64 ) old width
		fire.Width 	= math.Rand( 20, 50 )
		//fire.Height = math.Rand( 64, 96 ) old height
		fire.Height = math.Rand( 190, 290 )
		fire.DieTime 	= math.Rand( .5, 1 )
		fire.Frame 	= math.Rand( 80, 120 )

	table.insert( self.Fires, fire )
	
	for i=0,math.random(2,3) do
	
		local fire = {}
			fire.Pos 	= (self:GetPos() - Vector(0,0,32)) + Vector( math.Rand( -1, 1 ), math.Rand( -1, 1 ), 0)  * math.Rand( 16, 32 )
			fire.Width 	= math.Rand( 25, 45 )
			//fire.Height = math.Rand( 64, 96 ) old height
			fire.Height = math.Rand( 170, 270 )
			fire.DieTime 	= math.Rand( .5, 1 )
			fire.Frame 	= math.Rand( 80, 120 )
			
		table.insert( self.Fires, fire )
	end
end


include('shared.lua')